package site.xuqing.androidbase;

import android.text.TextUtils;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import site.xuqing.androidbase.net.BaseObserver;
import site.xuqing.androidbase.net.BaseResponse;
import site.xuqing.androidbase.net.OnResponse;
import site.xuqing.androidbase.net.flash.FlashViewFactory;
import site.xuqing.androidbase.net.flash.FlashViewFactory.FlashViewType;
import site.xuqing.androidbase.util.ToastUtil;

/**
 * @author xuqing
 * @Description 处理具体的请求，且不需要管过程
 * @createTime 2021年12月13日 15:33:52
 */
public class Requester {
    public static <T,R extends BaseResponse<T>> void sendRequest(Observable<R> observable, OnResponse<T> onResponse) {
        sendRequest(observable, null, null, null, onResponse);
    }
    
    public static <T,R extends BaseResponse<T>> void sendRequest(Observable<R> observable, Object flashView, OnResponse<T> onResponse) {
        sendRequest(observable, null, null, flashView, onResponse);
    }
    
    public static <T,R extends BaseResponse<T>>void sendRequest(Observable<R> observable, String errorText, OnResponse<T> onResponse) {
        sendRequest(observable, errorText, null, null, onResponse);
    }
    
    public static <T,R extends BaseResponse<T>> void sendRequest(Observable<R> observable, String errorText, String successText, OnResponse<T> onResponse) {
        sendRequest(observable, errorText, successText, null, onResponse);
    }
    
    public static <T,R extends BaseResponse<T>> void sendRequest(Observable<R> observable, String errorText, String successText, Object flashView, OnResponse<T> onResponse) {
        BaseObserver<T> observer = new BaseObserver<T>() {
            @Override
            public void onSuccess(BaseResponse<T> response) {
                onResponse.success(response.getData());
                if (!TextUtils.isEmpty(successText)) {
                    ToastUtil.showToast(successText);
                }
            }
            
            @Override
            public void onCodeError(BaseResponse<T> response) {
                onResponse.error();
                if (!TextUtils.isEmpty(errorText)) {
                    ToastUtil.showToast(errorText);
                } else {
                    ToastUtil.showToast(response.getMessage());
                }
            }
            
            @Override
            public void onFailure(Throwable e, boolean netWork){
                onResponse.error();
                if (!TextUtils.isEmpty(errorText)) {
                    ToastUtil.showToast(errorText);
                } else {
                    ToastUtil.showToast(e.getMessage());
                }
            }
        };
        if (flashView instanceof SwipeRefreshLayout) {
            observer.setFlashViewManager(FlashViewFactory.create(FlashViewType.Swipefreshlayout).setFlashView(flashView));
        } else if (flashView != null) {
            ToastUtil.showToast("暂时不支持除SwipeRefreshLayout之外的其他刷新框架");
        }
        observable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(observer);
    }
}
